package com.xingdata.jjxc.map.navi;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;

import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviGuide;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.autonavi.tbt.NaviStaticInfo;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.utils.Log;
import com.xingdata.jjxc.utils.SharedPreTools;

/**
 * 语音播报组件
 * 
 */
public class TTSController implements AMapNaviListener {
	public static TTSController ttsManager;
	private static String TAG = "TTSController";
	private Context mContext;
	// 合成对象.
	private SpeechSynthesizer mTts;

	// 缓冲进度
	private int mPercentForBuffering = 0;
	// 播放进度
	private int mPercentForPlaying = 0;
	// 语音听写UI
	private RecognizerDialog mIatDialog;
	// 默认发音人
	private String voicer = "xiaoyan";
	// 引擎类型
	private String mEngineType = SpeechConstant.TYPE_CLOUD;

	public TTSController(Context context) {
		this.mContext = context;
		init();
	}

	public static TTSController getInstance(Context context) {
		if (ttsManager == null) {
			ttsManager = new TTSController(context);
		}
		return ttsManager;
	}

	// public static TTSController getInstance(Context context) {
	// if (ttsManager == null) {
	// ttsManager = new TTSController(context);
	// }
	// return ttsManager;
	// }

	private void init() {
		// 初始化合成对象
		mTts = SpeechSynthesizer.createSynthesizer(mContext, mTtsInitListener);
		// 初始化听写Dialog，如果只使用有UI听写功能，无需创建SpeechRecognizer
		// 使用UI听写功能，请根据sdk文件目录下的notice.txt,放置布局文件和图片资源
		mIatDialog = new RecognizerDialog(mContext, mInitListener);
		// 设置参数
		setParam();
	}

	private void setParam() {

		// 清空参数
		mTts.setParameter(SpeechConstant.PARAMS, null);
		// 根据合成引擎设置相应参数
		if (mEngineType.equals(SpeechConstant.TYPE_CLOUD)) {
			mTts.setParameter(SpeechConstant.ENGINE_TYPE,
					SpeechConstant.TYPE_CLOUD);
			// 设置在线合成发音人
			mTts.setParameter(SpeechConstant.VOICE_NAME, voicer);
		} else {
			mTts.setParameter(SpeechConstant.ENGINE_TYPE,
					SpeechConstant.TYPE_LOCAL);
			// 设置本地合成发音人 voicer为空，默认通过语音+界面指定发音人。
			mTts.setParameter(SpeechConstant.VOICE_NAME, "");
		}
		// 设置合成语速
		mTts.setParameter(SpeechConstant.SPEED, "70");
		// 设置合成音调
		mTts.setParameter(SpeechConstant.PITCH, "50");
		// 设置合成音量
		mTts.setParameter(SpeechConstant.VOLUME, "50");
		// 设置播放器音频流类型
		mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");

		// 设置播放合成音频打断音乐播放，默认为true
		mTts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

		// 设置合成音频保存路径，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
		mTts.setParameter(SpeechConstant.PARAMS, "tts_audio_path="
				+ Environment.getExternalStorageDirectory() + "/test.pcm");

	}

	/**
	 * 初始化监听器。
	 */
	private InitListener mInitListener = new InitListener() {

		@Override
		public void onInit(int code) {
			Log.d(TAG, "SpeechRecognizer init() code = " + code);
			if (code != ErrorCode.SUCCESS) {
				Log.e(TAG, "初始化失败,错误码：" + code);
			}
		}
	};

	/**
	 * 初始化监听。
	 */
	private InitListener mTtsInitListener = new InitListener() {
		@Override
		public void onInit(int code) {
			Log.d(TAG, "InitListener init() code = " + code);
			if (code != ErrorCode.SUCCESS) {
				Log.e(TAG, "初始化失败,错误码：" + code);
			} else {
				// 初始化成功，之后可以调用startSpeaking方法
				// 注：有的开发者在onCreate方法中创建完合成对象之后马上就调用startSpeaking进行合成，
				// 正确的做法是将onCreate中的startSpeaking调用移至这里
			}
		}
	};

	/**
	 * 使用SpeechSynthesizer合成语音，不弹出合成Dialog.
	 * 
	 * @param
	 */
	public void playText(String playText) {
//		if (SharedPreTools.getString(mContext,Common.TTS_SWITCH, "9").equals("0")) {
			mTts.startSpeaking(playText, synthesizerListener);
//		}

	}

	public void stopSpeaking() {
		if (mTts != null)
			mTts.stopSpeaking();
	}

	// private void initSpeechSynthesizer() {
	// // 设置发音人
	// mSpeechSynthesizer.setParameter(SpeechConstant.VOICE_NAME,
	// mContext.getString(R.string.preference_default_tts_role));
	// // 设置语速
	// mSpeechSynthesizer.setParameter(SpeechConstant.SPEED,
	// "" + mContext.getString(R.string.preference_key_tts_speed));
	// // 设置音量
	// mSpeechSynthesizer.setParameter(SpeechConstant.VOLUME,
	// "" + mContext.getString(R.string.preference_key_tts_volume));
	// // 设置语调
	// mSpeechSynthesizer.setParameter(SpeechConstant.PITCH,
	// "" + mContext.getString(R.string.preference_key_tts_pitch));
	//
	// }

	public void destroy() {
		if (mTts != null) {
			mTts.stopSpeaking();
		}
	}

	@Override
	public void onArriveDestination() {
		// TODO Auto-generated method stub
		this.playText("到达目的地");
	}

	@Override
	public void onArrivedWayPoint(int arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onCalculateRouteFailure(int arg0) {
		this.playText("路径计算失败，请检查网络或输入参数");
	}

	@Override
	public void onCalculateRouteSuccess() {
	}

	@Override
	public void onEndEmulatorNavi() {
		this.playText("导航结束");
		Log.i(TAG, "导航结束");
		// BluetoothUpdataObserver.getCommunicationUpdata().CommunicationUpdata(
		// Bluetooth.endMark);
	}

	@Override
	public void onGetNavigationText(int arg0, String arg1) {
		this.playText(arg1);
	}

	@Override
	public void onInitNaviFailure() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onInitNaviSuccess() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLocationChange(AMapNaviLocation arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onReCalculateRouteForTrafficJam() {
		// TODO Auto-generated method stub
		this.playText("前方路线拥堵，路线重新规划");
	}

	@Override
	public void onReCalculateRouteForYaw() {
		this.playText("您已偏航");
	}

	@Override
	public void onStartNavi(int arg0) {

	}

	@Override
	public void onTrafficStatusUpdate() {

	}

	@Override
	public void onGpsOpenStatus(boolean arg0) {

	}

	@Override
	public void onNaviInfoUpdated(AMapNaviInfo arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNaviInfoUpdate(NaviInfo arg0) {

		// TODO Auto-generated method stub

	}

	public SynthesizerListener synthesizerListener = new SynthesizerListener() {

		@Override
		public void onSpeakResumed() {
			Log.i(TAG, "继续语音");
		}

		@Override
		public void onSpeakProgress(int arg0, int arg1, int arg2) {
		}

		@Override
		public void onSpeakPaused() {
			Log.i(TAG, "暂停语音");
		}

		@Override
		public void onSpeakBegin() {
		}

		@Override
		public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
		}

		@Override
		public void onCompleted(SpeechError arg0) {
		}

		@Override
		public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
		}
		
		
	};

	@Override
	public void OnUpdateTrafficFacility(TrafficFacilityInfo arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hideCross() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hideLaneInfo() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showCross(AMapNaviCross arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showLaneInfo(AMapLaneInfo[] arg0, byte[] arg1, byte[] arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	@Deprecated
	public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyParallelRoad(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onArriveDestination(NaviStaticInfo arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCalculateMultipleRoutesSuccess(int[] arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateAimlessModeStatistics(AimLessModeStat arg0) {
		// TODO Auto-generated method stub
		
	}

}
